require(['util', 'validator'], function () {
        var $securityCommentForm = $("#securityCommentForm");

        var seleObj = {},
            corpId = "";

        $("#securityCommentBnt").hide();

        //左侧树点击事件
        commonFun.leftTreeNodeClickObj['funCheckSecurityCommentSet'] = function(event, treeId, treeNode) {
            if(treeNode.type === 'CAR' || treeNode.type === 'DRIVER'){
                $.messager.alert("提示","请选择组织机构或线路，进行参数设置。","info");
                return;
            }
            seleObj = treeNode;
            corpId = treeNode.corpId;
            showInterface();
        };

        //根据后台返回的数据，动态生成界面
        var showInterface = function(){
            $.ajaxExt({
                url : "/driverComment/searchDriverCommentInfo.htm",
                data : {"corpId":corpId, "organId":seleObj.id, "qcommentTypeCodes":new Array("CMT00000").toString(), "qcodeValue":"CMT00004"}
            }).done(function(data){
                var currData = data;
                //显示界面
                $('#u_tip').remove();
                $securityCommentForm.empty();
                $("#securityCommentBnt").show();
                //根据数据渲染界面
                var seleObjId = seleObj.id,
                    currObjId = currData[0].organId;

                $securityCommentForm.append("<h1 class='line-title'>【"+seleObj.name+"】的安保行为评语设置信息</h1>"
                +"<input type='hidden' name='corpId' value='"+corpId+"'/>"
                +"<input type='hidden' name='seleId' value='"+seleObjId+"'/>"
                +"<input type='hidden' name='currId' value='"+currObjId+"'/>");
                $.each(currData,function(n,entity){
                    var params = window.parent.GLOBAL_PARAMS.DICT_CACHE[entity.paramCode];
                    var paramDsc = "";
                    $.each(params,function(n,param){
                        paramDsc = paramDsc + param.value + ", ";
                    });
                    paramDsc = paramDsc.substr(0,paramDsc.length-2);

                    var preview = entity.commentCntnt.format({'0':'一分公司','1':'18路','2':'张三','3':'2014-10-01~2014-11-01','4':'98','5':'25','6':'5','7':'2014-10-01 08:30:30'});

                    var content = "<div class='user-info-box'>"
                        + "<input type='hidden' name='commentIds' value='"+entity.commentId+"'/>"
                        +"<h2 class='title-style1'><span>评语等级："+entity.commentGrade+"</span></h2>"
                        + "<ul class='form-grid form-grid2 form-grid-nowrap clearfix '>"
                        + "<li>"
                        +"<label class='label-text'>评语名称：</label><input type='text' name='commentGradeDscs' maxlength='16' class='input-text' style='width:90px;' data-validator='{required:true}' placeholder='请输入评语名称' value='"+entity.commentGradeDsc+"'/>"
                        +"</li>"
                        +"<li>"
                        +"<label class='label-text'>等级范围：</label><input type='text' name='beginValues' class='input-text' style='width:100px;' data-validator='{required:true,number:true}' placeholder='请输入开始值' value='"+entity.beginValue+"'/> ~ <input type='text' name='endValues' class='input-text' style='width:100px;' data-validator='{required:true,number:true}' placeholder='请输入结束值' value='"+entity.endValue+"'/>"
                        +"</li>"
                        +"<li class='li-textarea mb0'>"
                        +"<label class='label-text vt'>评语内容：</label><textarea class='vt' name='commentCntnts' rows='3' cols='80' data-validator='{required:true}' placeholder='根据参数，设置评语内容。'>"+entity.commentCntnt+"</textarea>"
                        +"</li>"
                        + "<li class='mt5 h-auto'><label class='label-text'>参数说明：</label>"
                        + "<span class='span-text c-999'>" + paramDsc + "</span>"
                        + "</li>"
                        + "<li class='mb0 mt10 h-auto wrap' style='width: 800px;'>"
                        + "<label class='label-text'>预览效果：</label><span class='comment-preview'>" + preview + "</span>"
                        + "</li>"
                        + "<li class='mt5 h-auto'><label class='label-text'>备注：</label>"
                        + "<input type='text' name='remarks' maxlength='32' class='input-text' style='width:498px;' placeholder='输入备注信息。' value='"+(entity.remark==null?"":entity.remark)+"'/>"
                        + "</li>"
                        + "</ul>";
                        +"</div>"
                    $securityCommentForm.append(content);
                });
                $('textarea[name=commentCntnts]').on("change", function () {
                    var preview = $(this).val().format({
                        '0': '一分公司',
                        '1': '18路',
                        '2': '张三',
                        '3': '2014-10-01~2014-11-01',
                        '4': '98'
                    });
                    $(this).parent().parent().find(".comment-preview").html(preview);
                });
            });
        };

        $("#securityComment_update").on("click", function(){
            if($.validator($('#securityCommentForm'))){
                $.messager.confirm("是否修改","确定修改该机构的安保行为评语设置信息？",function(r){
                    if(r){
                        var formData = $securityCommentForm.serialize();
                        $.ajaxExt({
                            url : "/driverComment/uptDriverCommentInfo.htm",
                            data : formData
                        }).done(function(data){
                            showInterface();
                        });
                    }
                });
            }
        });

        $("#securityComment_reset").on("click", function(){
            showInterface();
        });

        String.prototype.format = function (param) {
            var value = String(this);
            var args = arguments,
                proto = Object.prototype.toString;

            if (args.length) {
                args = args.length == 1 ?
                    (param !== null && (/\[object Array\]|\[object Object\]/.test(proto.call(param))) ? param : args) : args;
                return value.replace(/\{(\w+)}/g, function (pattern, name) {
                    var val = args[name];
                    if (typeof val == 'function') {
                        val = val(name);
                    }
                    return("undefined" == typeof val ? "" : val)
                })
            }
            return value;
        };
        
        //页面打开自动查询
        var nodes = commonFun.getLeftTreeSelectedNodes();
    	if(nodes.length<1||nodes[0].type==="LINE"){
    		seleObj = nodes[0];
            corpId = nodes[0].corpId;
            showInterface();
    	}
})